package net.easyjava.base.utils.mob;

import com.alibaba.fastjson.JSONObject;
import net.easyjava.base.utils.mob.bean.CalendarBean;
import net.easyjava.base.utils.mob.bean.ResultBean;
import net.easyjava.base.utils.mob.constants.MobConstants;
import net.easyjava.base.utils.mob.constants.UrlConstants;
import net.easyjava.tools.http.HttpTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by wenxiaoning on 16/1/28.
 */
public class MobApiUtils {
    private static Logger logger = LoggerFactory.getLogger(MobApiUtils.class);
    public static final String APP_KEY = "f3b201ed9818";

    /**
     * 万年历接口
     * @param date yyyy-MM-dd
     * @return
     */
    public static CalendarBean getCalendar(String date){

        String url = String.format(UrlConstants.API_CALENDAR,APP_KEY,date);
        ResultBean resultBean = getDate(url);
        if(resultBean.isSuccess()){
            return JSONObject.parseObject(resultBean.getResult(),CalendarBean.class);
        }
        return null;
    }

    /**
     * mob公共api调用方法
     * @param url
     * @return
     */
    private static ResultBean getDate(String url){
        try {
            String data = HttpTools.get(url);
            return JSONObject.parseObject(data,ResultBean.class);
        } catch (IOException e) {
            logger.error(e.getMessage(),e.fillInStackTrace());
            return new ResultBean(MobConstants.MSG_ERROR);
        }
    }

    public static void main(String[] args) {
        ResultBean resultBean = getDate("http://apicloud.mob.com/appstore/calendar/day?key=f3b201ed9818&date=2015-05-01");
        System.out.println("resultBean.toString() = " + resultBean.toString());
        System.out.println("getCalendar(\"2016-01-29\") = " + getCalendar("2016-01-29").toString());
    }
}
